package src.leetcode.greedy.hard_0135;

public class candy {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.candy(new int[]{1,2,2}));
    }
}

class Solution {
    public int candy(int[] ratings) {
        int lastacs = 1;
        int lastdes = 0;
        int lasthighest = 1;
        int sum = 1;
        for(int i = 1; i < ratings.length; i++){

            if(ratings[i] > ratings[i-1]){
                lastdes = 0;
                lastacs++;
                sum += lastacs;
                lasthighest = lastacs;
            }else if(ratings[i] < ratings[i-1]){
                lastacs = 1;
                lastdes++;
                if(lastdes == lasthighest) lastdes++;
                sum += lastdes;
            }else{
                lastacs = 1;
                lastdes = 0;
                lasthighest = 1;
                sum += 1;
            }
        }
        return sum;
    }
}
